package org.abratuhi.kvngroupware.brainstorm.gui;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.SpringLayout;
import javax.swing.border.TitledBorder;

import org.abratuhi.kvngroupware.KVNGroupwareClientAction;
import org.abratuhi.kvngroupware.brainstorm.manager.BrainstormManager;
import org.abratuhi.kvngroupware.brainstorm.model.Brainstorm;
import org.abratuhi.kvngroupware.login.manager.LoginManager;
import org.abratuhi.kvngroupware.network.Message;
import org.abratuhi.kvngroupware.network.MessageSender;
import org.abratuhi.kvngroupware.spring.SpringUtilities;
import org.abratuhi.kvngroupware.user.model.User;
import org.abratuhi.kvngroupware.widget.AbstractWorkWidget;
import org.jdom.Element;

@SuppressWarnings("serial")
public class GuiBrainstorm extends AbstractWorkWidget implements KeyListener{
	LoginManager loginmanager;
	BrainstormManager brainstormmanager;
	
	public JMenuBar menubar = new JMenuBar();
	public JMenu menuAction = new JMenu("Action");
	public JMenuItem menuitemSwitch = new JMenuItem();
	public JMenuItem menuitemOverview = new JMenuItem();
	
	public JList listUsers = new JList(new String[0]);
	public JTextArea textareaSheet = new JTextArea();
	public JButton buttonCommit = new JButton();
	
	public GuiBrainstorm(String frameid, String frametitle, MessageSender sender, Properties locale, LoginManager loginmanager, BrainstormManager brainstormmanager){
		super(frameid, frametitle, sender, locale);
		this.loginmanager = loginmanager;
		this.brainstormmanager = brainstormmanager;
		
		buttonCommit.setText(locale.getProperty("brainstorm.button.commit_sheet"));
		menuAction.setText(locale.getProperty("brainstorm.menu.action"));
		menuitemOverview.setText(locale.getProperty("brainstorm.menuitem.view_brainstorm"));
		menuitemSwitch.setText(locale.getProperty("brainstorm.menuitem.switch_sheet"));
		
		listUsers.setPreferredSize(new Dimension(100, 200));

		textareaSheet.setRows(40);
		textareaSheet.setColumns(40);
		textareaSheet.setWrapStyleWord(true);
		textareaSheet.setLineWrap(true);
		
		textareaSheet.addKeyListener(this);
		
		buttonCommit.setAction(new CommitAction(locale.getProperty("brainstorm.button.commit_sheet"), null, null, null));
		menuitemSwitch.setAction(new SwitchAction(locale.getProperty("brainstorm.menuitem.switch_sheet"), null, null, null));
		menuitemOverview.setAction(new SwitchAction(locale.getProperty("brainstorm.menuitem.view_brainstorm"), null, null, null));
		
		menubar.add(menuAction);
		menuAction.add(menuitemSwitch);
		menuAction.add(menuitemOverview);

		JPanel users = new JPanel(new SpringLayout());
		users.setBorder(new TitledBorder(locale.getProperty("brainstorm.border.users")));
		users.add(new JScrollPane(listUsers));
		SpringUtilities.makeCompactGrid(users, 1, 1, 0, 0, 0, 0);

		JPanel send = new JPanel(new SpringLayout());
		send.add(buttonCommit);
		SpringUtilities.makeCompactGrid(send, 1, 1, 0, 0, 0, 0);

		JPanel chat = new JPanel(new SpringLayout());
		chat.setBorder(new TitledBorder(locale.getProperty("brainstorm.border.sheet")));
		chat.add(new JScrollPane(textareaSheet));
		chat.add(send);
		SpringUtilities.makeCompactGrid(chat, 2, 1, 0, 0, 0, 0);

		setLayout(new SpringLayout());
		setVisible(true);
		setPreferredSize(new Dimension(400, 400));
		add(new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, users, chat));
		SpringUtilities.makeCompactGrid(this, 1, 1, 0, 0, 0, 0);
		
	}
	
	public void paint(Graphics g){
		super.paint(g);
		
		User user = loginmanager.getUser();
		Brainstorm brainstorm = brainstormmanager.get(frameid);
		listUsers.setListData(brainstorm.getMemberIdsAsArray());
		textareaSheet.setText(brainstorm.getSheet(user.getId()));
		
		listUsers.repaint();
		textareaSheet.repaint();
	}
	
	public void backupText(){
		User user = loginmanager.getUser();
		Brainstorm brainstorm = brainstormmanager.get(frameid);
		brainstorm.updateSheet(user.getId(), textareaSheet.getText());
	}
	
	class CommitAction extends KVNGroupwareClientAction{
		public CommitAction(String text, ImageIcon icon, String desc,
				Integer mnemonic) {
			super(text, icon, desc, mnemonic);
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			User user = loginmanager.getUser();
			Brainstorm brainstorm = brainstormmanager.get(frameid);
			
			String[] attrNames = new String[]{"type", "action", "brainstormId", "userId", "teamId", "brainstormTheme"};
			String[] attrValues= new String[]{"brainstorm", "commit", brainstorm.getId(), user.getId(), brainstorm.getTeamId(), brainstorm.getTheme()};
			String text = textareaSheet.getText();
			Element[] content = new Element[0];
			
			Message message = sender.createMessage(attrNames, attrValues, text, content);
			sender.sendMessage(message);
		}
	}
	
	class SwitchAction extends KVNGroupwareClientAction{
		public SwitchAction(String text, ImageIcon icon, String desc,
				Integer mnemonic) {
			super(text, icon, desc, mnemonic);
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			User user = loginmanager.getUser();
			Brainstorm brainstorm = brainstormmanager.get(frameid);
			
			String[] attrNames = new String[]{"type", "action", "brainstormId", "userId", "teamId", "brainstormTheme"};
			String[] attrValues= new String[]{"brainstorm", "switch", brainstorm.getId(), user.getId(), brainstorm.getTeamId(), brainstorm.getTheme()};
			String text = textareaSheet.getText();
			Element[] content = new Element[0];
			
			Message message = sender.createMessage(attrNames, attrValues, text, content);
			sender.sendMessage(message);
		}
	}
	
	class OverviewAction extends KVNGroupwareClientAction{
		public OverviewAction(String text, ImageIcon icon, String desc,
				Integer mnemonic) {
			super(text, icon, desc, mnemonic);
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			GuiBrainstormOverview brainstormoverview = new GuiBrainstormOverview(frameid, "", sender, locale, brainstormmanager);
			brainstormoverview.createWindow();
		}
	}
	
	// TODO: add this action to window_closing event
	class LeaveAction extends KVNGroupwareClientAction{

		public LeaveAction(String text, ImageIcon icon, String desc,
				Integer mnemonic) {
			super(text, icon, desc, mnemonic);
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			User user = loginmanager.getUser();
			Brainstorm brainstorm = brainstormmanager.get(frameid);
			// commit before leave
			String[] attrNames = new String[]{"type", "action", "brainstormId", "userId", "teamId", "brainstormTheme"};
			String[] attrValues= new String[]{"brainstorm", "commit", brainstorm.getId(), user.getId(), brainstorm.getTeamId(), brainstorm.getTheme()};
			String text = textareaSheet.getText();
			Element[] content = new Element[0];
			
			Message message = sender.createMessage(attrNames, attrValues, text, content);
			sender.sendMessage(message);
			
			// leave
			attrNames = new String[]{"type", "action", "brainstormId", "userId", "teamId", "theme"};
			attrValues= new String[]{"brainstorm", "leave", brainstorm.getId(), user.getId(), brainstorm.getTeamId(), brainstorm.getTheme()};
			text = new String();
			content = new Element[0];
			
			message = sender.createMessage(attrNames, attrValues, text, content);
			sender.sendMessage(message);
		}
		
	}

	@Override
	public void keyPressed(KeyEvent e) {}
	@Override
	public void keyReleased(KeyEvent e) {}
	@Override
	public void keyTyped(KeyEvent e) {
		backupText();
	}
}
